package org.dromara.business.controller;

import java.math.BigDecimal;
import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.business.domain.vo.FundRewardVo;
import org.dromara.system.domain.bo.SysUserBo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.business.domain.vo.StakingProductVo;
import org.dromara.business.domain.bo.StakingProductBo;
import org.dromara.business.service.IStakingProductService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 质押产品
 *
 * @author Eric
 * @date 2025-09-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/stakingProducts")
public class StakingProductsController extends BaseController {

    private final IStakingProductService stakingProductsService;

    /**
     * 查询质押产品列表
     */
    @SaCheckPermission("business:stakingProducts:list")
    @GetMapping("/list")
    public TableDataInfo<StakingProductVo> list(StakingProductBo bo, PageQuery pageQuery) {
        return stakingProductsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出质押产品列表
     */
    @SaCheckPermission("business:stakingProducts:export")
    @Log(title = "质押产品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(StakingProductBo bo, HttpServletResponse response) {
        List<StakingProductVo> list = stakingProductsService.queryList(bo);
        ExcelUtil.exportExcel(list, "质押产品", StakingProductVo.class, response);
    }

    /**
     * 获取质押产品详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("business:stakingProducts:query")
    @GetMapping("/{id}")
    public R<StakingProductVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(stakingProductsService.queryById(id));
    }

    /**
     * 新增质押产品
     */
    @SaCheckPermission("business:stakingProducts:add")
    @Log(title = "质押产品", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody StakingProductBo bo) {
        return toAjax(stakingProductsService.insertByBo(bo));
    }

    @SaCheckPermission("business:stakingProducts:fundReward")
    @PostMapping("/fundReward")
    public R<Void> fundReward(@RequestBody @Validated FundRewardVo fundRewardVo){
        stakingProductsService.fundReward(fundRewardVo.getAmount());
        return toAjax(true);
    }

    /**
     * 修改质押产品
     */
    @SaCheckPermission("business:stakingProducts:edit")
    @Log(title = "质押产品", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody StakingProductBo bo) {
//        return toAjax(stakingProductsService.updateByBo(bo));
        return toAjax(true);
    }

    /**
     * 删除质押产品
     *
     * @param ids 主键串
     */
    @SaCheckPermission("business:stakingProducts:remove")
    @Log(title = "质押产品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(stakingProductsService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 状态修改
     */
    @SaCheckPermission("business:stakingProducts:edit")
    @Log(title = "质押产品", businessType = BusinessType.UPDATE)
    @PutMapping("/changeProductStatus")
    public R<Void> changeProductStatus(@RequestBody StakingProductBo bo) {
        return toAjax(stakingProductsService.changeProductStatus(bo.getId(), bo.getStatus()));
    }
}
